<template>
  <div class="container">
    <div class="title">我的热门活动（长按拖动可以排序）</div>
    <Draggable
      class="syllable_ul"
      element="ul"
      :list="List"
      :options="{group:'title', animation:150}"
      :no-transition-on-drag="true"
      @change="change"
    >
      <transition-group type="transition" :name="!drag? 'syll_li' : null" :css="true">
        <li v-for="(item , index) in List" :key="'idx'+index" class="listitem">
          <div>
            <img :src="item.activityImage+'?width=500&height=200'" alt class="listimg" />
          </div>
          <div @click="Delete(index)">
            <img src="./../assets/images/workspace/delet.png" alt />
          </div>
        </li>
      </transition-group>
    </Draggable>

    <div class="title" style="margin-top:0.2rem">未展示热门活动</div>
    <li v-for="(item , index) in zong" :key="'idx'+index" class="listitem">
           <div>
            <img :src="item.activityImage+'?width=500&height=200'"  alt class="listimg" />
          </div>
      <div @click="Add(index)">
        <img src="./../assets/images/workspace/add.png" alt />
      </div>
    </li>

    <div class="btn">
      <van-button type="default" class="submit" color="#DBB47F" @click="subMit()">提 交</van-button>
    </div>
  </div>
</template>
<script>
import Draggable from "vuedraggable";
import Api from "./../api/api";
import { Toast } from "vant";
export default {
  components: {
    Draggable
  },
  data() {
    return {
      List: [
    
      ],
      zong: [

      ],
      drag: false
    };
  },
  methods: {
    change(evt) {
     
    },
    Delete(i) {

      let { List, zong } = this;
      if( List.length == 1 ){
        Toast('至少保留一个活动')
        return;
      }
      List[i].showFlag = 0;
      zong.push(List[i]);
      List.splice(i, 1);
    },
    Add(i) {
      let { List, zong } = this;
      zong[i].showFlag = 1;
      List.push(zong[i]);
      zong.splice(i, 1);
    },
    subMit(){
        let { List, zong } = this;
        let arr = [];
        arr = arr.concat(List,zong);
        let subData = [];
        arr.forEach((el,index) =>{
          let obj = {
            activityId:el.id,
            sort:Number(index) +1,
            showFlag:el.showFlag,
          }  
          subData.push(obj);
        });

      this.$axios
        .post(Api.HTTP.updateSort,{
          employeeCode:localStorage.getItem('employeeCode'),
          datas:subData
        })
        .then(res => {
          console.log(res.data);
           Toast(res.data.msg); 
            setTimeout(()=>{
              this.$router.go(-1);
            },1000);
        })
        .catch(error => {
          Toast("网络错误");
          this.loadingShow = false;
        });

    }
  },

  mounted(){
    let datas = this.$route.query.data;

    datas.forEach(element => {
        if(element.showFlag == 1){
          this.List.push(element);
        }else{
           this.zong.push(element);
        }
    });
  }
};
</script>
<style scoped>
.listimg {
  width: 100%;
  height: 100%;
}
.container {
  width: 100%;
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  top: 0;
  padding-bottom: 1.2rem;
  background: #f6f5f5;
  overflow: hidden;
  overflow-y: auto;
}
.title {
  padding: 0.3rem 0.25rem 0.3rem 0.25rem;

  color: #666666;
  font-size: 0.25rem;
}
.listitem {
  padding: 0.25rem;
  border-bottom: 1px solid #f5f5f5;
  background: #fff;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.listitem div:nth-child(1) {
  width: 3rem;
  height: 1rem;
}

.listitem div:nth-child(2) {
  width: 0.3rem;
}
.listitem div:nth-child(2) img {
  width: 100%;
}
.btn {
  width: 100%;
  height: 1.2rem;
  padding: 0.2rem;
  box-sizing: border-box;
  background: #fff;
  position: fixed;
  bottom: 0;
}
.submit {
  width: 100%;
  height: 100%;
}
</style>